//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CallableFixedRateBond.h"
using namespace Cephei::QL::Experimental::Callablebonds;
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Instruments/Callability.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Experimental/Callablebonds/CallableBond.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::CCallableFixedRateBond (UInt32 settlementDays, Double faceAmount, Cephei::QL::Times::ISchedule^ schedule, Cephei::Core::IVector<Double>^ coupons, Cephei::QL::Times::IDayCounter^ accrualDayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<Double>^ redemption, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::Instruments::ICallability^>^>^ putCallSchedule, Cephei::QL::IPricingEngine^ QL_Pricer) : CCallableBond(CCallableFixedRateBond::typeid)
{
    CSchedule^ _Cschedule;
    CDayCounter^ _CaccrualDayCounter;
    try
    {
#ifdef HANDLE
        _phCallableFixedRateBond = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        QuantLib::Real _faceAmount = (QuantLib::Real)ValueHelper::Convert (faceAmount); //d
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        coupons->Lock();
        INativeVector<Double>^ _NCIcoupons = coupons->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcoupons = safe_cast<CDoubleVector^>(_NCIcoupons);
        std::vector<QuantLib::Rate>& _coupons = static_cast<std::vector<QuantLib::Rate>&> (_NCcoupons->GetReference ());
        _CaccrualDayCounter = safe_cast<CDayCounter^> (accrualDayCounter);
        _CaccrualDayCounter->Lock();
        QuantLib::DayCounter& _accrualDayCounter = static_cast<QuantLib::DayCounter&> (_CaccrualDayCounter->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>::IsSome::get (paymentConvention) ? (QuantLib::BusinessDayConvention)paymentConvention->Value : QuantLib::BusinessDayConvention::Following); //10
        QuantLib::Real _redemption = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (redemption) ? (QuantLib::Real)ValueHelper::Convert (redemption->Value) : 100.0); //4
        QuantLib::Date _issueDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (issueDate) ? (QuantLib::Date)ValueHelper::Convert (issueDate->Value) : QuantLib::Date()); //4
        CCallabilityVector^ _NCputCallSchedule;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::Instruments::ICallability^>^>::IsSome::get (putCallSchedule))
        {
            putCallSchedule->Value->Lock();
            INativeVector<Cephei::QL::Instruments::ICallability^>^ _NCIputCallSchedule = putCallSchedule->Value->getFeature (NativeFeature::shared_ptr);
            _NCputCallSchedule = safe_cast<CCallabilityVector^>(_NCIputCallSchedule);
        }
        std::vector<boost::shared_ptr<QuantLib::Callability> >& _putCallSchedule = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::Instruments::ICallability^>^>::IsSome::get (putCallSchedule) ? static_cast<std::vector<boost::shared_ptr<QuantLib::Callability> >&> (_NCputCallSchedule->GetShared ()) : QuantLib::CallabilitySchedule());//2
        _ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (new QuantLib::CallableFixedRateBond ( _settlementDays,  _faceAmount,  _schedule,  _coupons,  _accrualDayCounter,  _paymentConvention,  _redemption,  _issueDate,  _putCallSchedule ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCallableFixedRateBond)->setPricingEngine (_QL_Pricer);
        SetCallableBond (boost::dynamic_pointer_cast<QuantLib::CallableBond> (*_ppCallableFixedRateBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (coupons != nullptr) coupons->Unlock();    //not optional
        if (_CaccrualDayCounter != nullptr) _CaccrualDayCounter->Unlock();
        if (putCallSchedule != nullptr) putCallSchedule->Value->Unlock();
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::CCallableFixedRateBond (boost::shared_ptr<QuantLib::CallableFixedRateBond>& childNative, Object^ owner) : CCallableBond(CCallableFixedRateBond::typeid)
{
#ifdef HANDLE
	_phCallableFixedRateBond = NULL;
#endif
	_ppCallableFixedRateBond = &childNative;
    _ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (boost::dynamic_pointer_cast<QuantLib::CallableBond> (*_ppCallableFixedRateBond));
}
Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::CCallableFixedRateBond (QuantLib::CallableFixedRateBond& childNative, Object^ owner) : CCallableBond(CCallableFixedRateBond::typeid)
{
#ifdef HANDLE
	_phCallableFixedRateBond = NULL;
#endif
	_ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (&childNative);
    _ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (boost::dynamic_pointer_cast<QuantLib::CallableBond> (*_ppCallableFixedRateBond));
    _CallableFixedRateBondOwner = owner;
    _CallableBondOwner = owner;
}

Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::CCallableFixedRateBond (CCallableFixedRateBond^ copy) : CCallableBond(CCallableFixedRateBond::typeid)
{
#ifdef HANDLE
	_phCallableFixedRateBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (copy->GetShared());
        _ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (boost::dynamic_pointer_cast<QuantLib::CallableBond> (*_ppCallableFixedRateBond));
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::CCallableFixedRateBond (PLATFORM::Type^ t) : CCallableBond(CCallableFixedRateBond::typeid)
{
#ifdef HANDLE
	_phCallableFixedRateBond = NULL;
#endif
	if (!t->IsSubclassOf(CCallableFixedRateBond::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::CCallableFixedRateBond (QuantLib::Handle<QuantLib::CallableFixedRateBond>& childNative, Object^ owner)  : CCallableBond(CCallableFixedRateBond::typeid)
{
	_phCallableFixedRateBond = &childNative;
	_ppCallableFixedRateBond = &static_cast<boost::shared_ptr<QuantLib::CallableFixedRateBond>>(childNative.currentLink());
    _ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (boost::dynamic_pointer_cast<QuantLib::CallableBond> (*_ppCallableFixedRateBond));
    _CallableFixedRateBondOwner = owner;
}
Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::CCallableFixedRateBond (QuantLib::Handle<QuantLib::CallableFixedRateBond> childNative)  : CCallableBond(CCallableFixedRateBond::typeid)
{
	_phCallableFixedRateBond = &childNative;
	_ppCallableFixedRateBond = &static_cast<boost::shared_ptr<QuantLib::CallableFixedRateBond>>(childNative.currentLink());
    _ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (boost::dynamic_pointer_cast<QuantLib::CallableBond> (*_ppCallableFixedRateBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::CCallableFixedRateBond (QuantLib::CallableFixedRateBond childNative)  : CCallableBond(CCallableFixedRateBond::typeid)
{
#ifdef HANDLE
	_phCallableFixedRateBond = NULL;
#endif
	_ppCallableFixedRateBond = new boost::shared_ptr<QuantLib::CallableFixedRateBond> (new QuantLib::CallableFixedRateBond (childNative));
    _ppCallableBond = new boost::shared_ptr<QuantLib::CallableBond> (boost::dynamic_pointer_cast<QuantLib::CallableBond> (*_ppCallableFixedRateBond));
}
#endif

Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::~CCallableFixedRateBond ()
{
    if (_ppCallableFixedRateBond != NULL)
    {
	    delete _ppCallableFixedRateBond;
        _ppCallableFixedRateBond = NULL;
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::!CCallableFixedRateBond ()
{
    if (_ppCallableFixedRateBond != NULL)
    {
	    delete _ppCallableFixedRateBond;
    }
}
QuantLib::CallableFixedRateBond& Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::GetReference ()
{
    if (_ppCallableFixedRateBond == NULL) throw REFNEW NativeNullException ();
	return **_ppCallableFixedRateBond;
}
boost::shared_ptr<QuantLib::CallableFixedRateBond>& Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::GetShared ()
{
    if (_ppCallableFixedRateBond == NULL) throw REFNEW NativeNullException ();
	return *_ppCallableFixedRateBond;
}
QuantLib::CallableFixedRateBond* Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::GetPointer ()
{
    if (_ppCallableFixedRateBond == NULL) throw REFNEW NativeNullException ();
	return &**_ppCallableFixedRateBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CallableFixedRateBond>& Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::GetHandle ()
{
	if (_phCallableFixedRateBond == NULL)
	{
		_phCallableFixedRateBond = new Handle<QuantLib::CallableFixedRateBond> (*_ppCallableFixedRateBond);
	}
	return *_phCallableFixedRateBond;
}
#endif
bool Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond::HasNative () 
{
	return (_ppCallableFixedRateBond != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Callablebonds::ICallableFixedRateBond^ Cephei::QL::Experimental::Callablebonds::CCallableFixedRateBond_Factory::Create (UInt32 settlementDays, Double faceAmount, Cephei::QL::Times::ISchedule^ schedule, Cephei::Core::IVector<Double>^ coupons, Cephei::QL::Times::IDayCounter^ accrualDayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<Double>^ redemption, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Cephei::QL::Instruments::ICallability^>^>^ putCallSchedule, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CCallableFixedRateBond ( settlementDays,  faceAmount,  schedule,  coupons,  accrualDayCounter,  paymentConvention,  redemption,  issueDate,  putCallSchedule,  QL_Pricer);
}
